package cn.edu.ctbu.service.impl;

import cn.edu.ctbu.context.BaseContext;
import cn.edu.ctbu.entity.Category;
import cn.edu.ctbu.entity.Employee;
import cn.edu.ctbu.mapper.CategoryMapper;
import cn.edu.ctbu.service.ICategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-12
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> get(String name, Integer type) {
        QueryWrapper wrapper=new QueryWrapper<>();
        if(name!=null){
            wrapper.like("name",name);
        }
        if(type!=null){
            wrapper.eq("type",type);
        }
        List<Category> list = list(wrapper);
        return list;
    }

    @Override
    public void status(Long id) {
        Category byId = getById(id);
        Integer status = byId.getStatus();
        Category category=new Category();
        category.setId(id);
        if(status==1){
            category.setStatus(0);
        }else{
            category.setStatus(1);
        }
        Long currentId = BaseContext.getCurrentId();
        category.setUpdateUser(currentId);
        category.setUpdateTime(LocalDateTime.now());
        updateById(category);
    }

    @Override
    public void del(Long id) {
        removeById(id);
    }

    @Override
    public void add(Category category) {
        category.setCreateUser(BaseContext.getCurrentId()).setUpdateUser(BaseContext.getCurrentId())
                        .setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now());
        save(category);
    }

    @Override
    public void put(Category category) {
        category.setUpdateTime(LocalDateTime.now()).setUpdateUser(BaseContext.getCurrentId());
        updateById(category);
    }

    @Override
    public List<Category> type(Integer type) {
        QueryWrapper wrapper=new QueryWrapper<>().eq("type",type);
        List list = list(wrapper);
        return list;
    }

    @Override
    public List<Category> getCategory() {
        QueryWrapper wrapper=new QueryWrapper<>().eq("status",1);
        List list = list(wrapper);
        return list;
    }


}
